﻿using GalaSoft.MvvmLight;
using System.Collections.Generic;
using wp8skinswitcher.Model;
using System.Linq;

namespace wp8skinswitcher.ViewModel
{
    /// <summary>
    /// This class contains properties that the main View can data bind to.
    /// <para>
    /// See http://www.galasoft.ch/mvvm
    /// </para>
    /// </summary>
    public class MainViewModel : ViewModelBase
    {
        private readonly IPhoneSkinService _phoneService;

        /// <summary>
        /// Initializes a new instance of the MainViewModel class.
        /// </summary>
        public MainViewModel(IPhoneSkinService phoneService)
        {
            _phoneService = phoneService;

            _phoneService.GetPhoneSkins(
                (phoneSkins, error) =>
                {
                    if (error != null)
                    {
                        // Report error here
                        return;
                    }
                    WvgaPhones = phoneSkins.Where(s => s.Type == EmulatorType.wvga).ToList();
                    WxgaPhones = phoneSkins.Where(s => s.Type == EmulatorType.wxga).ToList();
                    S720pPhones = phoneSkins.Where(s => s.Type == EmulatorType.s720p).ToList();
                });
        }

        /// <summary>
        /// The <see cref="WvgaPhones" /> property's name.
        /// </summary>
        public const string WvgaPhonesPropertyName = "WvgaPhones";

        private List<PhoneSkin> _wvgaPhones = new List<PhoneSkin>();

        /// <summary>
        /// Sets and gets the WvgaPhones property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// </summary>
        public List<PhoneSkin> WvgaPhones
        {
            get
            {
                return _wvgaPhones;
            }

            set
            {
                if (_wvgaPhones == value)
                {
                    return;
                }

                RaisePropertyChanging(WvgaPhonesPropertyName);
                _wvgaPhones = value;
                RaisePropertyChanged(WvgaPhonesPropertyName);
            }
        }

        /// <summary>
        /// The <see cref="WxgaPhones" /> property's name.
        /// </summary>
        public const string WxgaPhonesPropertyName = "WxgaPhones";

        private List<PhoneSkin> _wxgaPhones = new List<PhoneSkin>();

        /// <summary>
        /// Sets and gets the WxgaPhones property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// </summary>
        public List<PhoneSkin> WxgaPhones
        {
            get
            {
                return _wxgaPhones;
            }

            set
            {
                if (_wxgaPhones == value)
                {
                    return;
                }

                RaisePropertyChanging(WxgaPhonesPropertyName);
                _wxgaPhones = value;
                RaisePropertyChanged(WxgaPhonesPropertyName);
            }
        }

        /// <summary>
        /// The <see cref="S720pPhones" /> property's name.
        /// </summary>
        public const string S720pPhonesPropertyName = "S720pPhones";

        private List<PhoneSkin> _720pPhones = new List<PhoneSkin>();

        /// <summary>
        /// Sets and gets the S720pPhones property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// </summary>
        public List<PhoneSkin> S720pPhones
        {
            get
            {
                return _720pPhones;
            }

            set
            {
                if (_720pPhones == value)
                {
                    return;
                }

                RaisePropertyChanging(S720pPhonesPropertyName);
                _720pPhones = value;
                RaisePropertyChanged(S720pPhonesPropertyName);
            }
        }

        /// <summary>
        /// The <see cref="StatusTextWvga" /> property's name.
        /// </summary>
        public const string StatusTextWvgaPropertyName = "StatusTextWvga";

        private string _statusTextWvga = "";

        /// <summary>
        /// Sets and gets the StatusTextWvga property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// </summary>
        public string StatusTextWvga
        {
            get
            {
                return _statusTextWvga;
            }

            set
            {
                if (_statusTextWvga == value)
                {
                    return;
                }

                RaisePropertyChanging(StatusTextWvgaPropertyName);
                _statusTextWvga = value;
                RaisePropertyChanged(StatusTextWvgaPropertyName);
            }
        }

        /// <summary>
        /// The <see cref="StatusTextWxga" /> property's name.
        /// </summary>
        public const string StatusTextWxgaPropertyName = "StatusTextWxga";

        private string _statusTextWxga = "";

        /// <summary>
        /// Sets and gets the StatusTextWxga property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// </summary>
        public string StatusTextWxga
        {
            get
            {
                return _statusTextWxga;
            }

            set
            {
                if (_statusTextWxga == value)
                {
                    return;
                }

                RaisePropertyChanging(StatusTextWxgaPropertyName);
                _statusTextWxga = value;
                RaisePropertyChanged(StatusTextWxgaPropertyName);
            }
        }

        /// <summary>
        /// The <see cref="StatusText720p" /> property's name.
        /// </summary>
        public const string StatusText720pPropertyName = "StatusText720p";

        private string _statusText720p = "";

        /// <summary>
        /// Sets and gets the StatusText720p property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// </summary>
        public string StatusText720p
        {
            get
            {
                return _statusText720p;
            }

            set
            {
                if (_statusText720p == value)
                {
                    return;
                }

                RaisePropertyChanging(StatusText720pPropertyName);
                _statusText720p = value;
                RaisePropertyChanged(StatusText720pPropertyName);
            }
        }

        /// <summary>
        /// The <see cref="SelectedWvga" /> property's name.
        /// </summary>
        public const string SelectedWvgaPropertyName = "SelectedWvga";

        private PhoneSkin _selectedWvga = null;

        /// <summary>
        /// Sets and gets the SelectedWvga property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// </summary>
        public PhoneSkin SelectedWvga
        {
            get
            {
                return _selectedWvga;
            }

            set
            {
                if (_selectedWvga == value)
                {
                    return;
                }

                StatusTextWvga = "Setting Emulator skin to " + value.Title + " " + value.SubTitle;
                bool skinSwitched = _phoneService.SetEmulatorPhoneSkin(value);

                if (skinSwitched)
                {
                    StatusTextWvga = "Setting Emulator skin to " + value.Title + " " + value.SubTitle + "- Done!";
                }
                else
                {
                    StatusTextWvga = "Error Setting Emulator skin to " + value.Title + " " + value.SubTitle;
                }

                RaisePropertyChanging(SelectedWvgaPropertyName);
                _selectedWvga = value;
                RaisePropertyChanged(SelectedWvgaPropertyName);
            }
        }

        /// <summary>
        /// The <see cref="Selected720" /> property's name.
        /// </summary>
        public const string Selected720PropertyName = "Selected720";

        private PhoneSkin _selected720 = null;

        /// <summary>
        /// Sets and gets the Selected720 property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// </summary>
        public PhoneSkin Selected720
        {
            get
            {
                return _selected720;
            }

            set
            {
                if (_selected720 == value)
                {
                    return;
                }

                StatusText720p = "Setting Emulator skin to " + value.Title + "" + value.SubTitle;
                bool skinSwitched = _phoneService.SetEmulatorPhoneSkin(value);

                if (skinSwitched)
                {
                    StatusText720p = "Setting Emulator skin to " + value.Title + " " + value.SubTitle + "- Done!";
                }
                else
                {
                    StatusText720p = "Error Setting Emulator skin to " + value.Title + " " + value.SubTitle;
                }


                RaisePropertyChanging(Selected720PropertyName);
                _selected720 = value;
                RaisePropertyChanged(Selected720PropertyName);
            }
        }

        /// <summary>
        /// The <see cref="SelectedWxga" /> property's name.
        /// </summary>
        public const string SelectedWxgaPropertyName = "SelectedWxga";

        private PhoneSkin _selectedWxga = null;

        /// <summary>
        /// Sets and gets the SelectedWxga property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// </summary>
        public PhoneSkin SelectedWxga
        {
            get
            {
                return _selectedWxga;
            }

            set
            {
                if (_selectedWxga == value)
                {
                    return;
                }

                StatusTextWxga = "Setting Emulator skin to " + value.Title + " " + value.SubTitle;
                bool skinSwitched = _phoneService.SetEmulatorPhoneSkin(value);

                if (skinSwitched)
                {
                    StatusTextWxga = "Setting Emulator skin to " + value.Title + " " + value.SubTitle + "- Done!";
                }
                else
                {
                    StatusTextWxga = "Error Setting Emulator skin to " + value.Title + " " + value.SubTitle;
                }

                RaisePropertyChanging(SelectedWxgaPropertyName);
                _selectedWxga = value;
                RaisePropertyChanged(SelectedWxgaPropertyName);
            }
        }
    }
}